package com.cqc.coordinatorlayoutdemo;

import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 主要是实现顶部固定的效果，其他的都没做处理
 * 效果：屏幕从上到下是：A-->B-->C,
 *      手指往上滑动控件C，会导致控件A移除屏幕，控件B顶部固定；
 *      手指往下滑动控件C，控件A再次出现
 * 要求：
 *  １　划出屏幕的控件必须设置app:layout_scrollFlags="scroll|enterAlways"
 *  2 顶部固定的控件不用设置app:layout_scrollFlags="scroll|enterAlways"
 *  3 上述的2个控件必须在AppBarLayout中
 *  4 滑动控件 必须设置app:layout_behavior="@string/appbar_scrolling_view_behavior"
 *  5 滑动的那个控件不能是listView
 *  6 A B C 3个控件必须在CoodinatorLayout中
 *
 *
 * */
public class MainActivity extends AppCompatActivity {

    private List<String> list = new ArrayList<>();
    private Toolbar toolBar;
    private TabLayout tabLayout;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        findViews();
        initViews();
    }

    private void initViews() {
        initToolbar();
        initTabLayout();
        initRecyclerView();
    }

    private void initToolbar() {

    }

    private void initTabLayout() {
        //设置mode
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //添加每个tab的文字
        tabLayout.addTab(tabLayout.newTab().setText("tab1"), 0);
        tabLayout.addTab(tabLayout.newTab().setText("tab2"), 1);
        tabLayout.addTab(tabLayout.newTab().setText("tab3"), 2);
    }

    private void initRecyclerView() {
        initData();

        recyclerView.setItemAnimator(new DefaultItemAnimator());//item动画有默认的
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
        MyAdapter adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
    }

    private void findViews() {
        toolBar = (Toolbar) findViewById(R.id.toolBar);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    }

    private void initData() {
        list.clear();
        for (int i = 0; i < 100; i++) {
            list.add("text" + i);
        }
    }


    public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView tv = new TextView(MainActivity.this);
            MyHolder holder = new MyHolder(tv);
            return holder;
        }

        @Override
        public void onBindViewHolder(MyHolder holder, int position) {
            holder.tv.setText(list.get(position));
            holder.tv.setTextSize(25);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        public TextView tv;

        public MyHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView;
        }
    }
}
